Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsData:[],
    waterprice:0,
    waterbill_category:[],
    goods_price:0,
    gift_num:0,
    lessprice:0,
    buy_num :0,
    total_num:0,
    bill_id:1,
    title:"",
  
  },
  
  waterTicketDetail: function (e) {
    //通过 e.currentTarget.dataset.**拿到当前的数据
    var waterbill_id = e.currentTarget.dataset.waterbill_id;
    var goods_name = e.currentTarget.dataset.goods_name;
    var goods_img = e.currentTarget.dataset.goods_img;
    var goods_price = e.currentTarget.dataset.goods_price;
    var waterprice = e.currentTarget.dataset.waterprice;
    var total_num = e.currentTarget.dataset.total_num;
    var title = e.currentTarget.dataset.title;
    var bill_id = this.data.bill_id;
  //将当前页面的值通过url地址传到下一个页面
    wx.navigateTo({
      url: '../warerTicket-detail/warerTicket-detail?goods_name=' + goods_name + '&goods_img=' + goods_img + '&goods_price=' + goods_price + '&waterbill_id=' + waterbill_id + '&title=' + title + '&total_num=' + total_num + '&waterprice=' + waterprice + '&bill_id=' + bill_id})
  },

 //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
  getParentArr: function (id) {
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];  //上一个页面
    var ticketList = prevPage.data.ticketList;
    for (var i = 0; i < ticketList.length;i++){
      //上一页通过地址将id传过来，通过id比较获得i 的唯一性
      if (id == ticketList[i]['id']){
        this.setData({
          goodsData: ticketList[i],
          buy_num: ticketList[i]['waterbill_category'][0]['buy_num'],
          total_num: ticketList[i]['waterbill_category'][0]['total_num'],
          gift_num: ticketList[i]['waterbill_category'][0]['gift_num'],
          goods_price: ticketList[i]['goods_price'],
          waterprice: ticketList[i]['waterbill_category'][0]['buy_num'] * ticketList[i]['goods_price'],
          lessprice: ticketList[i]['waterbill_category'][0]['gift_num'] * ticketList[i]['goods_price'],
          title: ticketList[i]['waterbill_category'][0]['title']
        })
      }
     
    }
    var waterbill_category = this.data.goodsData.waterbill_category;
  },
  //计算水票价钱和优惠
  countprice:function(e){
    var buy_num = e.currentTarget.dataset.buy_num;
    var gift_num = e.currentTarget.dataset.gift_num;
    var total_num = e.currentTarget.dataset.total_num; 
    var title = e.currentTarget.dataset.title; 
    var id = e.currentTarget.dataset.id;
    this.setData({
      buy_num: buy_num,
      total_num: total_num,
      gift_num: gift_num,
      title: title,
      waterprice: buy_num * this.data.goods_price,
      lessprice: gift_num * this.data.goods_price,
      bill_id:id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //接受上一个页面传过来的值，并将接受过来的id与上一个页面数组里面的id进行匹配，如果相等，
    //将上一个页面的内容赋值给当前页面的内容并进行渲染
    this.getParentArr(options.id);
  },
   
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})